<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class View extends CI_Controller {

	function View(){
		parent::__construct();
		
		$this->load->model('cms_model');
	}
	
	public function index(){
		//param
		$sid = $this->uri->segment(3);
		if(empty($sid))
			$sid = 's_001';
		
		//find template
		$tcontent = '';
		$tid = 't_001';
		$sql = "select t.tid, t.tcontent from qf_site s, qf_template t where s.sid='$sid' and s.disabled=0 and s.sitestatus=0 and s.templateid=t.tid limit 1";
		$querytpl = $this->db->query($sql);
		if($querytpl->num_rows() > 0){
			$row = $querytpl->row();
			$tcontent = $row->tcontent;
			$tid = $row->tid;
		}		
		if(empty($tcontent))
			return ;
		
		/*---the data to view---*/
		
		//menu columns
		$sql = "select * from qf_column where parentid='' and sid='$sid' and disabled='0' and display='0' and tag='menu' order by sortno asc";
		$menucols = $this->db->query($sql);
		$data['menucols'] = $menucols->result_array();
		
		/*----------------------*/		
		
		//view by smarty
		$tplfilename = $tid.'.tpl';
		$this->smarty->view($tplfilename,$data,False);		
		//$output = $this->smarty->view($tplfilename,$data,True);
		//echo ($output);	
		
		//top menu columns
		/*$sid = 's_00000000000000000000000000000000';		
		$sql = "select * from qf_column where parentid='' and sid='s_00000000000000000000000000000000' and disabled='0' and display='0' order by sortno asc";
		$data['queryColumns'] = $this->db->query($sql);
		
		//biz news
		$sql = "select cid from qf_column where parentid='' and sid='s_00000000000000000000000000000000' and cname='通知公告' and disabled='0'";
		$query = $this->db->query($sql);
		$cid_tz = "";
		if($query->num_rows() > 0){
			$row = $query->row();
			$cid_tz = $row->cid;
		}
		if(!empty($cid_tz)){
			
			$this->db->where('cid',$cid_tz);
			$this->db->where('disabled',0);
			$this->db->where('display',0);
			$this->db->limit(10);
			$this->db->order_by('sortno asc,updatetime desc');
			$data['querytongzhi'] = $this->db->get('qf_article');
			//$sql = "select * from qf_article where cid='c_e41449c0c031dc979817a7c1d8f37121' and disabled='0' and display='0' order by sortno,updatetime desc limit 10";
			//$data['querytongzhi'] = $this->db->query($sql);
		}
				
		$this->load->view('welcome_message',$data);
		*/
	}
	
	public function col(){
		//param
		$cid = $this->uri->segment(3);
		$pagenum = $this->uri->segment(4);
		//$tag = $this->uri->segment(5); //special process
		
		//find template
		$sid = 's_001';
		$tid='t_002';
		$tcontent = '';
		$sql = "select t.tid, t.tcontent, c.sid from qf_column c, qf_template t where c.cid='$cid' and c.disabled=0 and c.templateid=t.tid limit 1";
		$querytpl = $this->db->query($sql);
		if($querytpl->num_rows() > 0){
			$row = $querytpl->row();
			$tcontent = $row->tcontent;
			$tid = $row->tid;
			$sid = $row->sid;
		}
		if(empty($tcontent)){
			$tid='t_002';
			$sql = "select tid, tcontent from qf_template where tid='$tid'";
			$querytpl = $this->db->query($sql);
			if($querytpl->num_rows() > 0){
				$row = $querytpl->row();
				$tcontent = $row->tcontent;
			}
		}
		if(empty($tcontent))
			return ;
			
		/*---the data to view---*/
			
		$data['cid'] = $cid;
			
		//menu columns
		$sql = "select * from qf_column where parentid='' and sid='$sid' and disabled='0' and display='0' and tag='menu' order by sortno asc";
		$menucols = $this->db->query($sql);
		$data['menucols'] = $menucols->result_array();
						
		//column name
		$colname = '';
		$this->db->where('cid',$cid);
		$query =$this->db->get('qf_column');
		if($query->num_rows() > 0){
			$row = $query->row();
			$colname = $row->cname;
		}
		$data['colname'] = $colname;	
		
		//sub columns
		$sql = "SELECT * FROM qf_column where disabled=0 and parentid='$cid' order by sortno";
		$querysubcol = $this->db->query($sql);
		$data['subcol'] = $querysubcol->result_array();
		
		//articles
		if(empty($pagenum))
			$pagenum = 1;
		$pagesize = 15;		
		$start = $pagesize * ($pagenum-1);
		
		$sql = "select * from qf_article where cid='$cid' and disabled='0' order by sortno asc, updatetime desc";
		$sql .=" limit ".$start.",".$pagesize;
		$queryarts = $this->db->query($sql);
		$data['articles'] = $queryarts->result_array();
		
		//pager
		$pagecount = 0;		
		$rownum = 0;
		$sql = "select count(*) as c from qf_article where cid='$cid' and disabled='0' ";
		$querycount = $this->db->query($sql);
		if($querycount->num_rows() > 0){
			$row = $querycount->row();
			$rownum = $row->c;
		}
		$pagecount = ceil($rownum / $pagesize);			
		$pager = $this->cms_model->GetPagerHtml($pagenum,$pagecount);
		$data['pager'] = $pager;
		
		/*----------------------*/		
		
		//view by smarty
		$tplfilename = $tid.'.tpl';
		$this->smarty->view($tplfilename,$data,False);
	}
	
	public function art(){
		
	}
	
	public function render(){
		$id = $this->uri->segment(3);
		if(substr($id,0,2) == 'a_'){
			//choose template
			
			//jump
			$this->db->where('aid',$id);
			$data['query'] =$this->db->get('qf_article');
			$this->load->view('article0_view',$data);
		}
		
		if(substr($id,0,2) == 'c_'){
			//choose template
			
			//jump
			$this->db->where('cid',$id);
			$data['query'] =$this->db->get('qf_column');
			$this->load->view('column0_view',$data);
		}
	}
}
